package huobi.huobi;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpException;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huobi.common.api.HbdmRestApiV1;
import com.huobi.common.api.IHbdmRestApi;
import com.okcoin.commons.okex.open.api.bean.futures.result.Liquidation;
import com.okcoin.commons.okex.open.api.config.APIConfiguration;
import com.okcoin.commons.okex.open.api.enums.I18nEnum;
import com.okcoin.commons.okex.open.api.service.futures.FuturesMarketAPIService;
import com.okcoin.commons.okex.open.api.service.futures.impl.FuturesMarketAPIServiceImpl;

/**
 * Hello world!
 *
 */
public class App 
{
	
    final static int from = 1;
    final static int to = 0;
    final static int limit = 20;
    final static String LTC_INSTRUMENT_ID = "LTC-USD-190322";
    final static String ETH_INSTRUMENT_ID = "ETH-USD-190322";
    final static int LTC_VALID_EXPLOSION_SIZE = 1000;
    final static int ETH_VALID_EXPLOSION_SIZE = 1000;
    static int validExplosionNum = 0;
    static boolean placeOrder = false;
    //时间误差范围，5秒内
    static int timeTolerance = 5*1000;

    static String instrumentId = "LTC-USD-190329";
    
    public APIConfiguration config;
    
    public FuturesMarketAPIService marketAPIService;
    
    private static Logger logger = LoggerFactory.getLogger(App.class);
	
    public APIConfiguration config() {
        
    	config = new APIConfiguration();

        config.setEndpoint("https://www.okex.me/");

        // okex api和secret
        config.setApiKey("");
        config.setSecretKey("");


        config.setPassphrase("");
        config.setPrint(true);
        config.setReadTimeout(45);
        config.setI18n(I18nEnum.ENGLISH);

        return config;
    }
	
	public void init(){
		
		APIConfiguration config = config();
		
		marketAPIService = new FuturesMarketAPIServiceImpl(config);
		
	}
	
	public static void getWarehouseExplosion(){
 
		App app = new App();
    	
    	app.init();
    	
    	List<Liquidation> res = app.marketAPIService.getInstrumentLiquidation(LTC_INSTRUMENT_ID, 1, from, to, 100);
    	
    	for(int i=0; i < res.size(); i++){
    		Liquidation temp = res.get(i);
    		System.out.print("币："+ temp.getInstrument_id()+" ");
    		if(temp.getType() == 3){
    			System.out.print("爆仓类型："+ "卖出平多 ");
    		}else{
    			System.out.print("爆仓类型："+ "买入平空 ");
    		}
    		DateTime dt = new DateTime(temp.getCreated_at());
    		System.out.print("时间："+ dt.toDateTime() +" ");
    		System.out.print("价格："+ temp.getPrice()+" ");
    		System.out.println("数量："+ temp.getSize()+" ");
    	}

		
	}
	
    public static void main( String[] args )
    {
    	getWarehouseExplosion();
    }
    
    public static void test(String instrumentID, int validExplosionSize) throws InterruptedException{
		App app = new App();
    	app.init();
    	List<Liquidation> contractList;
    	int sizeCount = 0;
    	int queryCount = 0;
   	 	while(true){
   	 		sizeCount = 0;
   	 		placeOrder = false;
   	 		validExplosionNum = 0;
   	 		DateTime now = new DateTime();
   	 		long nowMillis = now.getMillis();
   	 		queryCount++;
   	 		logger.info("第 "+ queryCount+" 次查询爆仓信息");
   	 		contractList = app.marketAPIService.getInstrumentLiquidation(instrumentID, 1, from, to, limit);
   	 		for(int i=0;i<contractList.size();i++){
   	 			Liquidation temp = contractList.get(i);
   	 			DateTime dt = new DateTime(temp.getCreated_at());
   	 			if(nowMillis - dt.getMillis() > timeTolerance){
   	 				break;
   	 			}
   	 			if(temp.getSize() >= validExplosionSize){
   	 				placeOrder = true;
   	 			}
   	 			sizeCount += temp.getSize();
   	 			validExplosionNum++;
   	 		}
   	 		if( sizeCount >= validExplosionSize && validExplosionNum >= 3){
   	 			placeOrder = true;
   	 		}
   	 		if(placeOrder){
   	 			logger.info("开始下单");
   	 		}
   	 		Thread.sleep(1000);
   	 	}

    }
    
    
    public static void huobi() throws HttpException, IOException{

    	/**
		 * get请求无需发送身份认证,通常用于获取行情，市场深度等公共信息
		 */
		String api_key = ""; // huobi申请的apiKey,API调试过程中有问题或者有疑问可反馈微信号shaoxiaofeng1118
		String secret_key = ""; // huobi申请的secretKey
		String url_prex = "https://api.hbdm.com";//火币api接口地址https://api.hbdm.com
		IHbdmRestApi futureGetV1 = new HbdmRestApiV1(url_prex);
		IHbdmRestApi futurePostV1 = new HbdmRestApiV1(url_prex, api_key, secret_key);
		// 获取用户账户信息
		String accountInfo = futurePostV1.futureContractAccountInfo("EOS");
		logger.info("获取用户账户信息" + accountInfo);
		
		//合约下单
		String contractOrder = futurePostV1.futureContractOrder("EOS", "quarter", "EOS190329", "", "3.500", "3",
				"buy", "open", "10", "limit");

		logger.info("合约下单返回" + contractOrder);
		
		// 合约取消订单
		String contractcancel = futurePostV1.futureContractCancel("65", "","EOS");
		logger.info("合约取消订单" + contractcancel);
		
		
    }
}
